﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace SqlWatcher
{
    public static class SocketSendHelper
    {

        //private Socket _serverSocket;
        //private  IPAddress
        // public SocketSendHelper(IPAddress SrcIP,Int32 port

        public static void SendEvent(object input)//(object indata,IPAddress SrcIP,Int32 port)
        {
            Socket serverSocket = null;
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            ArrayList parameters = (ArrayList)input;
            List<object[]> EventQueue = (List<object[]>)parameters[0];
            //string message = (string)parameters[0];
            IPAddress ipAddress = (IPAddress)parameters[1];
            Int32 port = (Int32)parameters[2];

            // Send an event over a connection 
            // 
            try
            {

               //Console.WriteLine("On IP Address:" + ipAddress.ToString());


                IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, port);

                // Create a socket and listen for incoming connections.
                using (Socket listenSocket = new Socket(
                    AddressFamily.InterNetwork, SocketType.Stream,
                    ProtocolType.Tcp))
                {
                try
                    {    
                       listenSocket.Bind(ipEndpoint);
                        listenSocket.Listen(1);

                        // Accept a connection and create a socket to handle it.
                        serverSocket = listenSocket.Accept();
                       //Console.WriteLine("Server is connected.\n");
                

                
                        // Send data to the client.
                        //Console.Write("Sending data ... ");
                        // For each event returned from the query
                        foreach (var evt in EventQueue)
                        {
                            StringBuilder line = new StringBuilder();
                            for (int i=0; i < evt.Length;i++)
                            {
                                if (evt[i] != null) line.Append(evt[i].ToString());
                                line.Append("|");
                            }
                            string message = line.ToString().Replace("\r\n", " ") + "\n\r";
                           int bytesSent = serverSocket.Send(encoding.GetBytes(message), 0, message.Length, SocketFlags.None);
                           //Console.WriteLine(line.ToString().Replace("\r\n", " "));
                            
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + ex.StackTrace);
                    }

                    finally
                    {
                        serverSocket.Shutdown(SocketShutdown.Both);
                        //Console.WriteLine("Connection shut down.");
                        serverSocket.Close();
                    }
               }
 
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.StackTrace);
            }


        }
    }
}
